package com.yc.testException;

import java.io.IOException;

public class TryCatch {

	/**
	 * 测试try-finally是否会向上抛异常
	 */
	public void testTryFinally() throws IOException {
		try {
			throw new IOException("下层方法抛IOException异常了！");
		} finally {
			System.out.println("---------finally");
		}
	}
	
	public void testTryFinally2() {
		try {
			throw new RuntimeException("下层方法抛RuntimeException异常了！");
		} finally {
			System.out.println("---------finally2");
		}
	}
	
	/**
	 * Exception in thread "main" com.yc.testException.TestRuntimeException
		at com.yc.testException.TryCatch.testNullPoint(TryCatch.java:32)
		at com.yc.testException.Test.testNullPoint(Test.java:26)
		at com.yc.testException.Test.main(Test.java:38)
	 */
	public void testNullPoint() {
		try {
			String a = null;
			String b = a.substring(2);
			
		} catch (Exception e) {
			throw new TestRuntimeException();
		}
	}
	
	/**
	 * Exception in thread "main" com.yc.testException.TestRuntimeException: java.lang.NullPointerException
			at com.yc.testException.TryCatch.testNullPoint2(TryCatch.java:51)
			at com.yc.testException.Test.testNullPoint2(Test.java:31)
			at com.yc.testException.Test.main(Test.java:39)
		Caused by: java.lang.NullPointerException
			at com.yc.testException.TryCatch.testNullPoint2(TryCatch.java:48)
			... 2 more
	 */
	public void testNullPoint2() {
		try {
			String a = null;
			String b = a.substring(2);
			
		} catch (Exception e) {
			throw new TestRuntimeException(e);
		}
	}
	
	/**
	 * Exception in thread "main" com.yc.testException.TestRuntimeException: 错误说明
		at com.yc.testException.TryCatch.testNullPoint3(TryCatch.java:67)
		at com.yc.testException.Test.testNullPoint3(Test.java:36)
		at com.yc.testException.Test.main(Test.java:45)
	 */
	public void testNullPoint3() {
		try {
			String a = null;
			String b = a.substring(2);
			
		} catch (Exception e) {
			throw new TestRuntimeException("错误说明");
		}
	}

	public void testExc4() {
		System.out.println("-------1");
		System.out.println("-------2");
		try {
			System.out.println("-------3");
			int a = 0;
			int b = 3/2;
			System.out.println("-------4");
		} catch (Exception e) {
			System.out.println("----error---5--");
			throw new RuntimeException(e);
		}
		System.out.println("-------1");
		testExc5();
		System.out.println("-------1");
	}


	public void testExc5() {
		System.out.println("-------1");
		int a1 = 0;
		int b1 = 3/a1;
		System.out.println("-------2");
		try {
			System.out.println("-------3");
			int a = 0;
			int b = 3/a;
			System.out.println("-------4");
		} catch (Exception e) {
			System.out.println("----error---5--");
			throw new RuntimeException(e);
		}
		System.out.println("-------1");
		System.out.println("-------1");
	}

}
